/**
 * 公司：金丹科技
 * 文件：MyServiceConfig
 * 作者：kong
 * 时间：2019/4/17 9:11
 * 描述：
 */

package com.springboot.test.config;

import com.springboot.test.fifter.MyFilter;
import com.springboot.test.listener.MyListener;
import com.springboot.test.servlet.MyServlet;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Arrays;


@Configuration
public class MyServiceConfig extends WebMvcConfigurerAdapter {

//    配置嵌入式的servlet容器

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){

        return  new EmbeddedServletContainerCustomizer() {

            @Override
            public void customize(ConfigurableEmbeddedServletContainer configur) {
                configur.setPort(8030);
            }
        };
    }
// 注册三大组件
 @Bean
 public ServletRegistrationBean myservlet(){
     ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
     servletRegistrationBean.setLoadOnStartup(1);
     return servletRegistrationBean;
 }
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean myter = new FilterRegistrationBean();
        myter.setFilter(new MyFilter());
        myter.setUrlPatterns(Arrays.asList("/hello","/myServlet "));
        return myter;
    }

    @Bean
    public ServletListenerRegistrationBean my(){
      return new ServletListenerRegistrationBean<MyListener>(new MyListener());

    }




}
